#include "glWindow.h"

#include <gl/gl.h>
#include <gl/glu.h>

glWindow::glWindow()
{  
}

glWindow::~glWindow()
{
	if (m_hrc)
	{
		wglMakeCurrent(NULL, NULL);	  // free rendering memory
		wglDeleteContext(m_hrc);		  // Delete our OpenGL Rendering Context
	}

	if (m_hdc) 
		ReleaseDC(m_hwnd, m_hdc);			// Release our HDC from memory
}

void glWindow::Init(HWND hwnd)
{
	m_hwnd = hwnd;
	GetClientRect(m_hwnd, &m_Rect);	          //get rect into our handy global rect
	InitializeOpenGL(m_Rect.right, m_Rect.bottom);// initialise openGL

	//OpenGL settings
	glShadeModel(GL_SMOOTH);							    // Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);	  	// Black Background
	glClearDepth(1.0f);									      // Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);						    	// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);							    	// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	glEnable(GL_LIGHTING);

	// Turn on the lights!
	GLfloat Light_Ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
	GLfloat Light_Diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
	GLfloat Light_Position[]= {128.0f, 200.0f, 128.0f, 1.0f};

	glLightfv(GL_LIGHT1, GL_AMBIENT,  Light_Ambient);
	glLightfv(GL_LIGHT1, GL_DIFFUSE,  Light_Diffuse);
	glLightfv(GL_LIGHT1, GL_POSITION, Light_Position);
	glEnable(GL_LIGHT1);  
}

bool glWindow::SetThePixelFormat(HDC hdc) 
{ 
	PIXELFORMATDESCRIPTOR pfd = {0}; 
	int pixelformat; 

	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);	// Set the size of the structure
	pfd.nVersion = 1;							              // Always set this to 1
	// Pass in the appropriate OpenGL flags
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 
	pfd.dwLayerMask = PFD_MAIN_PLANE;			// standard mask (this is ignored anyway)
	pfd.iPixelType = PFD_TYPE_RGBA;				// RGB and Alpha pixel type
	pfd.cColorBits = COLOUR_DEPTH;				// Here we use our #define for the color bits
	pfd.cDepthBits = COLOUR_DEPTH;				// Ignored for RBA
	pfd.cAccumBits = 0;						      	// nothing for accumulation
	pfd.cStencilBits = 0;					      	// nothing for stencil

	//Gets a best match on the pixel format as passed in from device
	if ( (pixelformat = ChoosePixelFormat(hdc, &pfd)) == false ) 
	{ 
		MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK); 
		return false; 
	} 

	//sets the pixel format if its ok. 
	if (SetPixelFormat(hdc, pixelformat, &pfd) == false) 
	{ 
		MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK); 
		return false; 
	}  
	return true;
}

void glWindow::ResizeGLWindow(int width, int height)// Initialize The GL Window
{
	if (height==0)// Prevent A Divide By Zero error
	{
		height=1;   // Make the Height Equal One
	}

	glViewport(0,0,width,height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	//calculate aspect ratio
	gluPerspective(70.0f,(GLfloat)width/(GLfloat)height, 1.0f,10000.0f);

	glMatrixMode(GL_MODELVIEW);// Select The Modelview Matrix
	glLoadIdentity();          // Reset The Modelview Matrix
}

void glWindow::InitializeOpenGL(int width, int height) 
{  
	m_hdc = GetDC(m_hwnd);            //  sets  global HDC

	if (!SetThePixelFormat(m_hdc))    //  sets  pixel format
		PostQuitMessage (0);

	m_hrc = wglCreateContext(m_hdc);	//  creates  rendering context from  hdc
	wglMakeCurrent(m_hdc, m_hrc);		  //	Use this HRC.

	ResizeGLWindow(width, height);	  // Setup the Screen
}